window.uam = window.uam || {};
uam.Utils = {
		
	/* ************************
	 * Response Codes 
	 * ***********************/
	responseCode : {
		OK : 200,
		ERROR : 500,
		// invalid request code
		ERROR_INVALID_REQUEST_CODE : 501,
		ERROR_INVALID_PARAMETERS : 502
	},
	
	/**
	 * ajax call to task
	 * 
	 * @param {Map} params: Object request parameter list
	 * @param {Function} succcess: Function handle the callback
	 * 
	 */
	callTask : function(params, success){
		
		if(typeof(microstrategy) != 'undefined'){
			var session = String(microstrategy.sessionState);
		}
		
		params = params || {};
		
		params["taskId"] = params["taskId"] || 'N001';
		params["taskEnv"] = params["taskEnv"] || 'xhr';
        params["taskContentType"] = params["taskContentType"] || 'json';
        params["session"] = params["session"] || String(session);
        params["dateTime"] = params["dateTime"] || (new Date()).getTime();
        //alert(params["searchDBFld"]);
        
        //formValueSubmit:
        
        success = success || function(data){ console ? console.log("callTask.data = " + data) : alert("callTask.data = " + data); }
		
        console.log(params);
        
		taskUrl = './taskProc';
		$.ajax({
			url:  taskUrl,
			type: 'GET',
			data: params,
			cache: "false",
			dataType: "text",
			async: true, //set to false in case we want it to wait
			success: success ,
			error: function (XMLHttpRequest, textStatus, errorThrown) {
		        alert("XMLHttpRequest="+XMLHttpRequest.responseText+"\ntextStatus="+textStatus+"\nerrorThrown="+errorThrown);
		    } 
		});
	},
	
	// MM-dd-yyyy
	date2String: function (date) {
		var year = date.getFullYear();
	    var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
	    var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
	    return month + "-" + day + "-" + year;
	},
	// MM-dd-yyyy HH:mm:ss
	datetime2String: function (datetime){
	    var year = datetime.getFullYear();
	    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
	    var day = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
	    var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours();
	    var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
	    var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
	    return month + "-" + day + "-" + year + " " + hour + ":" + minute + ":" + second;
	},
	// 
	trim : function(str, def) {
		if (!str)  {
			return def != 'undefined' ? def : null; 
		}
    	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  	},
  	/**
  	 * @param {String} source
  	 * @param {String} start
  	 * @return {boolean} return true if 'source' start with 'start'
  	 */
  	startWith : function(source, start){
  		if (source && start) {
			return source.indexOf(start) == 0;
  		}
  		return false;
	},  
	/**
	 * 
	 * @param {} sourceStr
	 * @param {} endStr
	 * @return {boolean} return true if 'source' end with 'end'
	 */
	endWith : function(source, end){
		if (source && end) {
			var i = source.length - end.length;
    		return (i >= 0 && source.lastIndexOf(end) == i)	
		}
		return false;
	},
	startProgress : function() {
		var progress = $(".loading");
		if (progress.length == 0) {
			$("body").append("<div class='loading'>loading...</div>");
		} else {
			progress.css("display", "block");
		}
	},
	stopProgress : function(){
		var progress = $(".loading");
		if (progress.length > 0) {
			progress.css("display", "none");
		}
	}

};
uam.ShoppingCart = {
	select : function (src, dest, out, changeListener) {
		var destList = document.getElementById(dest);
		var srcList = document.getElementById(src);
		
		var len = destList.length;
		for ( var i = 0; i < srcList.length; i++) {
			if ((srcList.options[i] != null) && (srcList.options[i].selected)) {

				var found = false;
				for ( var count = 0; count < len; count++) {
					if (destList.options[count] != null) {
						if (srcList.options[i].text == destList.options[count].text) {
							found = true;
							break;
						}
					}
				}
				if (found != true) {
					destList.options[len] = new Option(srcList.options[i].text, srcList.options[i].value);
					destList.options[len].ondblclick = function(){uam.ShoppingCart.remove(src, dest, out, changeListener)};
					//destList.options[len].selected = true;
					len++;
				}
			}
		}
		uam.ShoppingCart.output(dest, out);
		// call change listener
		changeListener(out);
	},
	remove : function (src, dest, out, changeListener) {
		var destList = document.getElementById(dest);
		
		var len = destList.options.length;
		for ( var i = (len - 1); i >= 0; i--) {
			if ((destList.options[i] != null) && (destList.options[i].selected == true)) {
				destList.options[i] = null;
			}	
		}
		uam.ShoppingCart.output(dest, out);
		// call change listener
		changeListener(out);
	},
	output : function(dest, out){
		var outCtrl = document.getElementById(out);
		var destCtrl = document.getElementById(dest)
		var options = destCtrl.options;
		outCtrl.value = "";
		for(var i = 0; i<options.length; i++) {
			outCtrl.value += options[i].value;
			if( i + 1 < options.length) {
				outCtrl.value += ",";
			}
		}
		destCtrl.focus();
	}
	
};

uam.utils = uam.utils || uam.Utils;